package com.example.smart_city_0829.mainFragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.DeathData;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.activity.sdkm.ActivitiesActivity;
import com.example.smart_city_0829.activity.sdkm.DetailActivity;
import com.example.smart_city_0829.activity.sdkm.SbdyxActivity;
import com.example.smart_city_0829.activity.sdkm.XxxdActivity;
import com.example.smart_city_0829.base.BaseFragment;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.sdkm.Kmlb;
import com.example.smart_city_0829.view.MaxSizeLv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.List;
import java.util.stream.Collectors;

public class FuPinFragment extends BaseFragment {

    private Banner banner;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;
    private MaxSizeLv kmlv;
    private MaxSizeLv yxlv;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fupin;
    }

    @Override
    protected void initView() {
        banner = (Banner) rootView.findViewById(R.id.banner);
        textView3 = (TextView) rootView.findViewById(R.id.textView3);
        textView4 = (TextView) rootView.findViewById(R.id.textView4);
        textView5 = (TextView) rootView.findViewById(R.id.textView5);
        kmlv = (MaxSizeLv) rootView.findViewById(R.id.kmlv);
        yxlv = (MaxSizeLv) rootView.findViewById(R.id.yxlv);

    }

    @Override
    protected void initData() {
        List<Kmlb> kmlb = DeathData.kmlb;
        List<Kmlb> yxlb = DeathData.yxgs;

        banner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object o, ImageView imageView) {
                        Glide.with(context)
                                .load(o)
                                .into(imageView);
                    }
                }).setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
                .setImages(kmlb.stream().map(Kmlb::getPic).collect(Collectors.toList()))
                .setBannerTitles(kmlb.stream().map(Kmlb::getTitle).collect(Collectors.toList()))
                .start()
                .startAutoPlay();

        kmlv.setAdapter(new CommonAdapter<Kmlb, CommonAdapter.CommListViewHolder>(kmlb,requireContext()) {
            @Override
            protected void setData(Kmlb data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
                Glide.with(context)
                        .load(data.getPic())
                        .into(viewHolder.im);
                viewHolder.title.setText(data.getTitle());
                viewHolder.content.setText(data.getContent());
                viewHolder.tag1.setText(data.getName());
            }

            @Override
            protected CommListViewHolder getViewHolder() {
                return new CommListViewHolder();
            }

            @Override
            protected void initView(CommListViewHolder viewHolder, View convertView) {
                viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
            }
        });
        yxlv.setAdapter(new CommonAdapter<Kmlb, CommonAdapter.CommListViewHolder>(yxlb,requireContext()) {
            @Override
            protected void setData(Kmlb data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
                Glide.with(context)
                        .load(data.getPic())
                        .into(viewHolder.im);
                viewHolder.title.setText(data.getTitle());
                viewHolder.content.setText(data.getContent());
                viewHolder.tag1.setText(data.getName());
            }

            @Override
            protected CommListViewHolder getViewHolder() {
                return new CommListViewHolder();
            }

            @Override
            protected void initView(CommListViewHolder viewHolder, View convertView) {
                viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
            }
        });
    }

    @Override
    protected void initEvent() {
        kmlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DeathData.kmlb.get(position).setLearned(true);
                Intent intent = new Intent(requireContext(), DetailActivity.class);
                intent.putExtra("data",gson.toJson(DeathData.kmlb.get(position)));
                startActivity(intent);
            }
        });

        yxlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DeathData.yxgs.get(position).setLearned(true);
                Intent intent = new Intent(requireContext(), DetailActivity.class);
                intent.putExtra("data",gson.toJson(DeathData.yxgs.get(position)));
                startActivity(intent);
            }
        });

        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                migrateTo(XxxdActivity.class);
            }
        });

        textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                migrateTo(ActivitiesActivity.class);
            }
        });

        textView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                migrateTo(SbdyxActivity.class);
            }
        });
    }
}
